package com.example.administrator.szb.fragments.fragment_forCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.WTContentActivity;
import com.example.administrator.szb.adapter.UCenterAskAdapter;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.bean.AskBase;
import com.example.administrator.szb.bean.AskBean;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stone on 2018/1/5.
 */

public class UCenterAsk extends MVPBaseFragment {
    private int types;//1我提出的问题2我收到的问题3我回答的问题
    private SmartRefreshLayout refreshLayout;
    private ListView listView;
    private boolean isFirstEnter=true;
    private int current_page = 1;//列表当前页
    private int per_page = 10;//默认展示条数
    private UCenterAskAdapter adapter;
    private List lists=new ArrayList();
    private int uid;
    private View error_net_ll;
    private Button home_fragment_button_djcs_error;
    private TextView tv_net_error;
    private ImageView iv_error2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shoucang_jg,null);
    }

    public void setType(int type,int id) {
        this.types = type;
        uid = id;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        home_fragment_button_djcs_error = view.findViewById(R.id.home_fragment_button_djcs_error);
        home_fragment_button_djcs_error.setVisibility(View.GONE);
        tv_net_error = view.findViewById(R.id.tv_net_error);
        iv_error2  = view.findViewById(R.id.iv_error2);
        iv_error2.setImageResource(R.mipmap.no_data_icon);
        tv_net_error.setText("暂无数据");
        error_net_ll = view.findViewById(R.id.error_net_ll);
        listView = view.findViewById(R.id.listView);
        refreshLayout = view.findViewById(R.id.zx_fragment_swiperefreshlayout);
    }

    @Override
    protected void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), WTContentActivity.class);
                //SampleApplicationLike.getQueueInstance().cancelAll("tag");
                intent.putExtra("refer_id", ((AskBean)lists.get(position)).getId());
                startActivity(intent);
            }
        });
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int p = current_page+1;
                requestReferList(1, p,per_page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requestReferList(0, 1,per_page);
            }
        });

    }

    @Override
    protected void initData() {
        adapter = new UCenterAskAdapter(lists,context);

        listView.setAdapter(adapter);
        if (isFirstEnter) {
            isFirstEnter = false;
            refreshLayout.autoRefresh();
        }
    }

    /**
     *
     * @param type 其中传入0代表刷新，1代表加载更多
     * @param page
     * @param count
     */
    private void requestReferList(final int type, int page, int count){
        error_net_ll.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("users_id", uid);
        params.put("current_page",page);
        params.put("genre",types);
        params.put("current_page",page);
        params.put("per_page",per_page);
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_WDGL_New, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                AskBase shouCangBean = JSON.parseObject(str,new TypeReference<AskBase>(){});
                String strJson="";
                if (types==1){
                    strJson = shouCangBean.getPutForward();
                    if (TextUtils.isEmpty(strJson)){
                        strJson = "[]";
                    }
                }else if (types==3){
                    strJson = shouCangBean.getAnswer();
                    if (TextUtils.isEmpty(strJson)){
                        strJson = "[]";
                    }
                }

                List list = JSON.parseArray(strJson, AskBean.class);
                if (type==1){
                    //加载更多
                    refreshLayout.finishLoadmore();

                    current_page++;
                    if (list.size()<per_page){

                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);

                    }
                }else {
                    lists.clear();

                    //刷新
                    refreshLayout.finishRefresh();
                    refreshLayout.setEnableLoadMore(true);
                    if (list.size()<per_page){

                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);

                    }
                    current_page=1;
                }
                lists.addAll(list);
                if (lists.size()==0){
                    listView.setVisibility(View.GONE);
                    error_net_ll.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(int requestCode, String str) {
                listView.setVisibility(View.GONE);
                error_net_ll.setVisibility(View.VISIBLE);
            }
        });
    }

}
